/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package maxent.mr.mapred;

import java.util.HashMap;
import maxent.mr.io.DeltaLUtil;
import stanford.tagger.LambdaSolveTagger;

/**
 *
 * @author hadoop
 */
public class IISUpdater {

    private LambdaSolveTagger prob;
    private HashMap<Integer, Double> listDeltaL = new HashMap<Integer, Double>();

    public IISUpdater() {
    }

    public IISUpdater(LambdaSolveTagger prob, String uri) {
        this.prob = prob;
        // reading deltaL from file
        listDeltaL = DeltaLUtil.read(uri);
    }



    public LambdaSolveTagger update(){
        
        for(int i=0;i<prob.lambda.length;i++){

            double deltaL = listDeltaL.get(i);
            
            if(updateLambda(i, deltaL)){
                prob.updateConds(i, deltaL);
            }
            else{
                //numNConverged--;
            }
        }
        
        return prob;
    }

    private boolean updateLambda(int index, double deltaL){
        
        prob.lambda[index] = prob.lambda[index] + deltaL;

        if(!(deltaL == deltaL)){
            System.out.println("NaN " + index + " "+deltaL);
        }
        
        return (Math.abs(deltaL)>= prob.eps);
    }


}
